import type { Effect, Reducer, Subscription } from 'umi';
import type { ReactElement } from 'react';
import type { ModalProps } from 'antd/es/modal';

enum Type {
  info,
  warning,
  other,
}
export interface AlertModelState extends ModalProps {
  isAlert: boolean;
  method?: string;
  text?: string;
  children?: ReactElement;
  btnList?: (string | Function)[][];
}
export interface AlertModelType {
  namespace: 'alert';
  state: AlertModelState;
  effects: {
    setAlert: Effect;
  };
  reducers: {
    onSuccess: Reducer<AlertModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<UserModelState>;
  };
  subscriptions: { setup: Subscription };
}
const AlertModel: AlertModelType = {
  namespace: 'alert',
  state: {
    isAlert: false,
    method: Type[0],
    btnList: undefined,
    text: '',
  },
  effects: {
    *setAlert({ payload }, { put }) {
      yield put({
        type: 'onSuccess',
        payload: { ...payload },
      });
    },
  },
  reducers: {
    onSuccess(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        dispatch({
          type: 'setAlert',
          payload: {
            isAlert: false,
            btnList: [],
            text: '',
            method: Type[0],
          },
        });
      });
    },
  },
};
export default AlertModel;
